package advancedpart.assignmentA2;

import static org.junit.Assert.*;

import java.io.IOException;
import java.util.HashMap;

import shared.*;

import org.junit.Test;

public class SearcherA2Test {	
	private WordList wordWithNoUrls1 = new WordList("wordWithNoUrls1", null, null);
	private WordList wordWithNoUrls2 = new WordList("wordWithNoUrls2", null, null);
	private WordList wordContaining11 = new WordList("wordContaining11", new URLList("1", null), null);
	private WordList wordContaining12 = new WordList("wordContaining12", new URLList("1", null), null);
	private WordList wordContaining2 = new WordList("wordContaining2", new URLList("2", null), null);
	private WordList wordContaining1and2 = new WordList("wordContaining1and2", new URLList("2", new URLList("1", null)), null);
	
	HashMap<String, WordList> wordMap = new HashMap<String, WordList>();
	
	/*Test of exists*/
	@Test
	public void testExistsA() throws IOException {
		assertEquals(true, SearcherA2.exists(SearcherA2.createWordMap(dataSetF), "your"));
	}

	@Test
	public void testExistsB() throws IOException {
		assertEquals(false, SearcherA2.exists(SearcherA2.createWordMap(dataSetF), "spacedude"));
	}

	/*Test of getUrlsForOneWord */
	@Test
	public void testGetUrlsForOneWordA() {
		wordMap.clear();
		wordMap.put("wordContaining1and2", wordContaining1and2);
		assertEquals("2\n1\n", SearcherA2.getUrlsForOneWord(wordMap, "wordContaining1and2"));

	} 

	@Test
	public void testGetUrlsForOneWordB() {
		wordMap.clear();
		wordMap.put("wordContaining11", wordContaining11);
		assertEquals("", SearcherA2.getUrlsForOneWord(wordMap, "not"));

	}

	@Test
	public void testGetUrlsForOneWordC() {
		wordMap.clear();
		wordMap.put("wordWithNoUrls1", wordWithNoUrls1);
		assertEquals("", SearcherA2.getUrlsForOneWord(wordMap, "wordWithNoUrls1"));

	}

	/*Test of getUrlsForTwoWords*/
	private SearchOperator andOperator = SearchOperator.AND;
	private SearchOperator orOperator = SearchOperator.OR;
	
	@Test
	public void testGetUrlsForTwoWordsA() {
		wordMap.put("wordWithNoUrls1", wordWithNoUrls1);
		wordMap.put("wordWithNoUrls2", wordWithNoUrls2);
		assertEquals("", SearcherA2.getUrlsForTwoWords(wordMap, "wordWithNoUrls1", "wordWithNoUrls2", andOperator));

	}
	
	@Test
	public void testGetUrlsForTwoWordsB() {
		wordMap.clear();
		wordMap.put("wordWithNoUrls1", wordWithNoUrls1);
		wordMap.put("wordWithNoUrls2", wordWithNoUrls2);
		assertEquals("", SearcherA2.getUrlsForTwoWords(wordMap, "wordWithNoUrls1", "wordWithNoUrls2", orOperator));

	}
	
	@Test
	public void testGetUrlsForTwoWordsD() {
		wordMap.clear();
		wordMap.put("wordContaining11", wordContaining11);
		wordMap.put("wordWithNoUrls2", wordWithNoUrls2);
		assertEquals("", SearcherA2.getUrlsForTwoWords(wordMap, "wordContaining11", "wordWithNoUrls2", andOperator));

	}
	
	@Test
	public void testGetUrlsForTwoWordsE() {
		wordMap.clear();
		wordMap.put("wordContaining11", wordContaining11);
		wordMap.put("wordContaining2", wordContaining2);
		assertEquals("", SearcherA2.getUrlsForTwoWords(wordMap, "wordContaining11", "wordContaining2", andOperator));

	}
	
	@Test
	public void testGetUrlsForTwoWordsF() {
		wordMap.clear();
		wordMap.put("wordContaining11", wordContaining11);
		wordMap.put("wordContaining12", wordContaining12);
		assertEquals("1\n", SearcherA2.getUrlsForTwoWords(wordMap, "wordContaining11", "wordContaining12", andOperator));

	}
	
	@Test
	public void testGetUrlsForTwoWordsG() {
		wordMap.clear();
		wordMap.put("wordContaining11", wordContaining11);
		wordMap.put("wordWithNoUrls2", wordWithNoUrls2);
		assertEquals("1\n", SearcherA2.getUrlsForTwoWords(wordMap, "wordContaining11", "wordWithNoUrls2", orOperator));

	}
	
	@Test
	public void testGetUrlsForTwoWordsH() {
		wordMap.clear();
		wordMap.put("wordWithNoUrls1", wordWithNoUrls1);
		wordMap.put("wordContaining2", wordContaining2);
		assertEquals("2\n", SearcherA2.getUrlsForTwoWords(wordMap, "wordWithNoUrls1", "wordContaining2", orOperator));

	}
	
	@Test
	public void testGetUrlsForTwoWordsI() {
		wordMap.clear();
		wordMap.put("wordContaining11", wordContaining11);
		wordMap.put("wordContaining2", wordContaining2);
		assertEquals("1\n2\n", SearcherA2.getUrlsForTwoWords(wordMap, "wordContaining11", "wordContaining2", orOperator));

	}
	
	@Test
	public void testGetUrlsForTwoWordsJ() {
		wordMap.clear();
		wordMap.put("wordContaining11", wordContaining11);
		wordMap.put("wordContaining1and2", wordContaining1and2);
		assertEquals("1\n2\n", SearcherA2.getUrlsForTwoWords(wordMap, "wordContaining11", "wordContaining1and2", orOperator));

	}

	/*Test of createWordMap*/
	//Test data sets:
	String dataSetA = "data/test/dataSetA.txt";		
	String dataSetB = "data/test/dataSetB.txt";		
	String dataSetC = "data/test/dataSetC.txt";		
	String dataSetD = "data/test/dataSetD.txt";
	String dataSetE = "data/test/dataSetE.txt";		
	String dataSetF = "data/test/dataSetF.txt";
	
	@Test
	public void testCreateWordMapA () throws IOException {
		wordMap.clear();
		assertEquals(wordMap, SearcherA2.createWordMap(dataSetA));
	}
	
	@Test
	public void testCreateWordMapB () throws IOException {
		wordMap.clear();
		assertEquals(wordMap, SearcherA2.createWordMap(dataSetB));
	}
	
	@Test
	public void testCreateWordMapC () throws IOException {
		wordMap.clear();
		wordMap.put("your", new WordList("your",new URLList("1", null),null));
		assertEquals(wordMap, SearcherA2.createWordMap(dataSetC));
	}
	
	@Test
	public void testCreateWordMapD () throws IOException {
		wordMap.clear();
		wordMap.put("your", new WordList("your",new URLList("1", null),null));
		wordMap.put("bases", new WordList("bases",new URLList("1", null),null));
		wordMap.put("are", new WordList("are",new URLList("1", null),null));
		wordMap.put("belong", new WordList("belong",new URLList("1", null),null));
		wordMap.put("to", new WordList("to",new URLList("1", null),null));
		wordMap.put("us", new WordList("us",new URLList("1", null),null));
		assertEquals(wordMap, SearcherA2.createWordMap(dataSetD));
	}
	
	@Test
	public void testCreateWordMapE () throws IOException {
		wordMap.clear();
		wordMap.put("your", new WordList("your",new URLList("1", null),null));
		assertEquals(wordMap, SearcherA2.createWordMap(dataSetE));
	}
	
	@Test
	public void testCreateWordMapF () throws IOException {
		wordMap.clear();
		wordMap.put("your", new WordList("your",new URLList("2", new URLList("1", null)),null));
		wordMap.put("bases", new WordList("bases",new URLList("2", new URLList("1", null)),null));
		wordMap.put("are", new WordList("are",new URLList("1", null),null));
		wordMap.put("belong", new WordList("belong",new URLList("1", null),null));
		wordMap.put("to", new WordList("to",new URLList("1", null),null));
		wordMap.put("us", new WordList("us",new URLList("1", null),null));
		assertEquals(wordMap, SearcherA2.createWordMap(dataSetF));
	}
}
